const { User, Word } = require('./module');
const io = require('socket.io');
const bcrypt = require('bcrypt');
const rooms = [{roomname: 'roomname',
    password: 'password',
    roomnum: 'socket.id',
    users: [
        {
            socketid: 'socket.id',
            username: 'username',
            draws:[],
            guessword:[],
        }
    ]}]; // roomname,password,roomnum,users:[{socketid,username}]
const user = [];
const word = [{ wordname: 'animal', wordscript: 'isanimal', wordcontent: ['bird', 'dog', 'cat'], worduser: 'a1' }]

function initializeSocketIO(server) {
    const socketServe = io(server)
    socketServe.on('connection', (socket) => {
        console.log('建立链接')
        
        socket.on('create_room', (formDate) => {
            console.log('createroom')
            const newRoom = {
                roomname: formDate.roomname,
                password: formDate.password,
                roomnum: socket.id+'room',
                users: [
                    {
                        socketid: socket.id,
                        username: formDate.username,
                        guessword: [],
                        draws: []
                    }
                ]
            }
            rooms.push(newRoom)
            socket.join(newRoom.roomnum)
            //console.log(rooms)
            socket.emit('newRoom',newRoom)
            socketServe.to(newRoom.roomnum).emit('playrooms', rooms)
        })

        socket.on('join_room', (roomnum,username) => {
            console.log('joinroom')
            socket.join(roomnum)
            const newUser = {
                socketid: socket.id,
                username: username,
                guessword: [],
                draws: []
            }
            const room = rooms.find(room => room.roomnum === roomnum)
            const users = room.users
            users.push(newUser)
            socketServe.to(roomnum).emit('joinroom', room)
        })

        socket.on('select_word', () => {
            console.log('sendwordlist')
            socket.emit('words', word)
        })

        socket.on('new_word', (newword) => {
            console.log('newWord')
            const newWord = {
                wordname: newword.wordname,
                wordscript: newword.wordscript,
                wordcontent: newword.wordcontent,
                worduser: newword.worduser
            }
            word.push(newWord)
        })
        socket.on('send_word', (word,roomnum) => {
            console.log(word)
            const arr = word.split(" ");
            const room = rooms.find(room => room.roomnum === roomnum)
            var i=0
            while(room.users[i]!=null){
            room.users[i].guessword.push(arr[i])
            socketServe.to(room.users[i].socketid).emit('getword', arr[i],room.users[i].username)
            console.log(room.users[i])
            i++
        }
        })

        socket.on('send_draw', (lines,roomnum,owner) => {
            const room = rooms.find(room => room.roomnum === roomnum)
            if (room) {
                const users = room.users;
                var currentIndex = users.findIndex(user => user.socketid === socket.id);
                var owneruser = users.findIndex(user => user.username === owner);
                users[owneruser].draws.push(lines)
                console.log(users[owneruser])
                if (currentIndex !== -1) {
                  const nextIndex = (currentIndex + 1) % users.length;
                  const nextUser = users[nextIndex];
                  socketServe.to(nextUser.socketid).emit('getline', lines,owner);
                  currentIndex = nextIndex 
                }}  
        })

        socket.on('finish_turn', (roomnum) => {
            const room = rooms.find(room => room.roomnum === roomnum)
            socketServe.to(roomnum).emit('finishturn');
        })

        socket.on('guess_word', (word,roomnum,owner) => {
            const room = rooms.find(room => room.roomnum === roomnum)
            if (room) {
                const users = room.users;
                var owneruser = users.findIndex(user => user.username === owner);
                users[owneruser].guessword.push(word)
                console.log(users[owneruser])
                var currentIndex = users.findIndex(user => user.socketid === socket.id);
                
                if (currentIndex !== -1) {
                  const nextIndex = (currentIndex + 1) % users.length;
                  const nextUser = users[nextIndex];
                  socketServe.to(nextUser.socketid).emit('getNewWord', word,owner);
                  currentIndex = nextIndex 
                }}
        })

        socket.on('get_history', (roomnum,username) => {
            const room = rooms.find(room => room.roomnum === roomnum)
            if (room) {
                const users = room.users;
                var owneruser = users.findIndex(user => user.username === username);
                console.log(users[owneruser])
                socketServe.to(socket.id).emit('gethistory', users[owneruser]);
                }
        })
    });
}
module.exports = {initializeSocketIO,rooms}